package thread.pool.core.executor;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.TimeUnit;

/**
 * 线程池构建辅助类
 */
public class ThreadPoolBuilder {

    private final DynamicThreadPoolExecutor threadPoolHelper;


    public static ThreadPoolBuilder builder() {
        return new ThreadPoolBuilder(new DynamicThreadPoolExecutor());
    }

    public DynamicThreadPoolExecutor build() {
        return this.threadPoolHelper;
    }

    /**
     * 拒绝策略
     */
    public ThreadPoolBuilder setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
        threadPoolHelper.setRejectedExecutionHandler(rejectedExecutionHandler);
        return this;
    }

    /**
     * 线程池名称
     */
    public ThreadPoolBuilder setPoolName(String poolName) {
        threadPoolHelper.setPoolName(poolName);
        return this;
    }

    /**
     * 核心线程数
     */
    public ThreadPoolBuilder setCorePoolSize(int coreSize) {
        threadPoolHelper.setCorePoolSize(coreSize);
        return this;
    }

    /**
     * 最大线程数
     */
    public ThreadPoolBuilder setMaxPoolSize(int maxPoolSize) {
        threadPoolHelper.setMaxPoolSize(maxPoolSize);
        return this;
    }

    /**
     * 线程池队列大小
     */
    public ThreadPoolBuilder setDequeSize(int dequeSize) {
        threadPoolHelper.setDequeSize(dequeSize);
        return this;
    }

    /**
     * 存活时间
     */
    public ThreadPoolBuilder setKeepAliveTime(long keepAliveTime) {
        threadPoolHelper.setKeepAliveTime(keepAliveTime);
        return this;
    }

    /**
     * 时间单位
     */
    public ThreadPoolBuilder setUnit(TimeUnit timeUnit) {
        threadPoolHelper.setUnit(timeUnit);
        return this;
    }

    /**
     * 队列任务告警阀值
     *
     * @param alarmSize 大小
     */
    public ThreadPoolBuilder setAlarmSize(int alarmSize) {
        threadPoolHelper.setAlarmSize(alarmSize);
        return this;
    }

    /**
     * 线程池任务描述 用于告警时用
     *
     * @param taskDescribe 描述
     */
    public ThreadPoolBuilder setTaskDescribe(String taskDescribe) {
        threadPoolHelper.setTaskDescribe(taskDescribe);
        return this;
    }

    private ThreadPoolBuilder(DynamicThreadPoolExecutor threadPoolHelper) {
        this.threadPoolHelper = threadPoolHelper;
    }
}
